package com.sinog.front.controller.biz;

import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import com.sinog.front.feign.basic.UserService;
import com.sinog.front.feign.biz.LogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @desc 系统管理 - 系统日志 - 系统日志 - 系统操作日志控制器 - SystemLogController
 * @author lzq
 * @date 2020-07-10 8:10 下午
 */
@Slf4j
@RestController
public class SystemLogController {

    @Autowired
    private LogService logService;
    @Autowired
    private UserService userService;

    /**
     * 系统管理 - 系统日志 - 系统日志 - 列表
     * @param searchUsername searchUsername
     * @param searchDepartName searchDepartName
     * @param operaType operaType
     * @param searchTime searchTime
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @return String
     */
    @RequestMapping("/azbj/azbjgl/systemlog/list")
    public String list(String searchUsername,String searchDepartName,String operaType,String searchTime,Integer pageIndex,Integer pageSize) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Result result1 = userService.selectOne(authentication.getName(),"0");
        UserDTO userDTO;
        if(Constants.Number.NUM_ONE.equals(result1.getCode())) {
            String data = (String) result1.getData();
            userDTO = JsonUtil.toBean(data, UserDTO.class);
        } else {
            userDTO = new UserDTO();
        }
        String roleid = userDTO.getRoleid();
        if(Constants.RoleId.ROLE_ID_AB.equals(roleid) || Constants.RoleId.ROLE_ID_SFT.equals(roleid)) {
            if(Constants.NumberStr.NUM_STR_TWO.equals(operaType)) {
                operaType = "新增";
            } else if(Constants.NumberStr.NUM_STR_THREE.equals(operaType)) {
                operaType = "更新";
            } else if(Constants.NumberStr.NUM_STR_FOUR.equals(operaType)) {
                operaType = "删除";
            } else if(Constants.NumberStr.NUM_STR_ONE.equals(operaType)) {
                operaType = "";
            }
            Map<String,String> map = new HashMap<>(16);
            map.put("username",searchUsername);
            map.put("departName",searchDepartName);
            map.put("type",operaType);
            Date date = null;
            if(!StringUtils.isEmpty(searchTime)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    date = sdf.parse(searchTime);
                } catch(ParseException e) {
                    log.error("发生异常:",e);
                }
                map.put("createTime",sdf.format(date));
            } else {
                map.put("createTime","");
            }
            return logService.list(map,pageIndex,pageSize);
        }
        return ReturnCode.NO_PERMISSION.message();
    }
}